﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using ArkanoidX.UI;
using ArkanoidX.Objects;

namespace ArkanoidX
{
    public class LevelTransition : BaseComponent
    {
        Level _source;
        Level _target;
        LevelBuilder _targetBuilder;

        bool _started = false;
        bool _finished = false;
        TimeSpan _initialTime;
        TimeSpan _minTime;

        Thread _builderThread;

        MovingText _levelClearText;

        public bool Finished
        {
            get
            {
                return _finished;
            }
        }

        public Level TargetLevel
        {
            get
            {
                return _target;
            }
        }

        public LevelTransition(Game game, 
            Level from, LevelBuilder to, TimeSpan minTime) : base(game)
        {
            _source = from;
            _targetBuilder = to;
            _minTime = minTime;

            _levelClearText = new MovingText((MainGame)game);
            _levelClearText.Font = game.Content.Load<SpriteFont>("Levels");
            if (_source == null)
            {
                _levelClearText.Value = String.Format("Loading level {0} ... ", _targetBuilder.LevelNumber);
                _levelClearText.Align(((MainGame)Game).GameRectangle, Window.AlignType.Center, 0, 0);
                _levelClearText.InflationRate = 1;
            }
            else
            {
                _levelClearText.Value = String.Format("Level {0} Clear", _source.LevelNumber);
                _levelClearText.Align(((MainGame)Game).Arena, Window.AlignType.Center, 0, 0);
                _levelClearText.InflationRate = 1.08f;
            }
            _levelClearText.Color = Color.Red;

        }

        public void Build()
        {
            _target = _targetBuilder.Build();

            if (_source != null)
            {
                _target.Lives = _source.Lives;
                _target.Score = _source.Score;
            }
            else
            {
                _target.Lives = MainGame.TotalLives;
                _target.Score = 0;
            }

        }

        public void Start()
        {
            this.AddToLayer(((MainGame)Game).BaseLayer);
            _levelClearText.Show();

            _builderThread = new Thread(new ThreadStart(Build));
            _builderThread.Start();
        }

        public override void Update(GameTime gameTime)
        {
            if (!_started)
            {
                _initialTime = gameTime.TotalGameTime;
                _started = true;
            }
            if (!_builderThread.IsAlive)
            {
                TimeSpan timePassed = gameTime.TotalGameTime - _initialTime;
                if (timePassed >= _minTime)
                {
                    _finished = true;
                    this.RemoveFromGame();
                    _levelClearText.RemoveFromGame();
                }
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }
    }
}
